package ui.tool;

import ent.Menu;
import ent.MenuItem;
import util.Find;
import util.SuperView;

/**
 *
 * @author t
 */
public class MenuManageView extends SuperView {

    private static MenuManageView instance = new MenuManageView();

    public static void display() {
        instance.setVisible(true);
    }

    public static MenuManageView getInstance() {
        return instance;
    }

    public MenuManageView() {
        onLoad();
    }

    /**
     * Creates new form CustomerView
     */
    public MenuManageView(java.awt.Frame parent, boolean modal) {
        this();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        menuAssignButton = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        inactiveMenuList = new javax.swing.JList();
        menuRemoveButton = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        activeMenuList = new javax.swing.JList();
        jPanel3 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        menuItemRemoveButton = new javax.swing.JButton();
        menuItemassignButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        activeMenuItemList = new javax.swing.JList();
        jScrollPane4 = new javax.swing.JScrollPane();
        inactiveMenuItemList = new javax.swing.JList();
        jLabel2 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("MENU MANAGEMENT");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Menu"));

        jLabel1.setText("Active:");

        jLabel3.setText("Inactive:");

        menuAssignButton.setText("<< Assign");
        menuAssignButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuAssignButtonActionPerformed(evt);
            }
        });

        jScrollPane3.setViewportView(inactiveMenuList);

        menuRemoveButton.setText("Remove >>");
        menuRemoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuRemoveButtonActionPerformed(evt);
            }
        });

        activeMenuList.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                activeMenuListMouseClicked(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                activeMenuListMouseReleased(evt);
            }
        });
        activeMenuList.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                activeMenuListKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(activeMenuList);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 201, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(menuRemoveButton)
                    .addComponent(menuAssignButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel3)
                    .addComponent(jScrollPane3, 0, 0, Short.MAX_VALUE))
                .addContainerGap())
        );

        jPanel2Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jScrollPane1, jScrollPane3});

        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(1, 1, 1)
                        .addComponent(jLabel3)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(menuAssignButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(menuRemoveButton)))
                .addContainerGap())
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Menu Item"));

        jLabel4.setText("Inactive:");

        menuItemRemoveButton.setText("Remove >>");
        menuItemRemoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemRemoveButtonActionPerformed(evt);
            }
        });

        menuItemassignButton.setText("<< Assign");
        menuItemassignButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                menuItemassignButtonActionPerformed(evt);
            }
        });

        jScrollPane2.setViewportView(activeMenuItemList);

        jScrollPane4.setViewportView(inactiveMenuItemList);

        jLabel2.setText("Active:");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(menuItemRemoveButton)
                            .addComponent(menuItemassignButton)))
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 198, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap())
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {menuItemRemoveButton, menuItemassignButton});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(menuItemassignButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 151, Short.MAX_VALUE)
                        .addComponent(menuItemRemoveButton))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void activeMenuListMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_activeMenuListMouseClicked
        fillMenuItemList();
}//GEN-LAST:event_activeMenuListMouseClicked

    private void activeMenuListKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_activeMenuListKeyReleased
        fillMenuItemList();
}//GEN-LAST:event_activeMenuListKeyReleased

    private void menuItemassignButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemassignButtonActionPerformed
        menuItemOperation(1);
    }//GEN-LAST:event_menuItemassignButtonActionPerformed

    private void menuItemRemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuItemRemoveButtonActionPerformed
        menuItemOperation(0);
    }//GEN-LAST:event_menuItemRemoveButtonActionPerformed

    private void menuAssignButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuAssignButtonActionPerformed
        menuOperation(1);
    }//GEN-LAST:event_menuAssignButtonActionPerformed

    private void menuRemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_menuRemoveButtonActionPerformed
        menuOperation(0);
    }//GEN-LAST:event_menuRemoveButtonActionPerformed

    private void activeMenuListMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_activeMenuListMouseReleased
        fillMenuItemList();
}//GEN-LAST:event_activeMenuListMouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                MenuManageView dialog = new MenuManageView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList activeMenuItemList;
    private javax.swing.JList activeMenuList;
    private javax.swing.JList inactiveMenuItemList;
    private javax.swing.JList inactiveMenuList;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JButton menuAssignButton;
    private javax.swing.JButton menuItemRemoveButton;
    private javax.swing.JButton menuItemassignButton;
    private javax.swing.JButton menuRemoveButton;
    // End of variables declaration//GEN-END:variables

    protected void onLoad() {
        initComponents();
        refresh();
    }

    private void refresh() {
        fillMenuList();
        fillMenuItemList();
        mainView.menuRegister();
    }

    private void fillMenuList() {
        activeMenuList.setListData(Find.menuByStatus(1).toArray());
        inactiveMenuList.setListData(Find.menuByStatus(0).toArray());
    }

    private void fillMenuItemList() {
        Object o = activeMenuList.getSelectedValue();
        if (o == null) {
            activeMenuItemList.setListData(new Object[]{});
            inactiveMenuItemList.setListData(new Object[]{});
            return;
        }
        Menu menu = (Menu) o;
        menu = m.find(Menu.class, menu.getMenu());

        activeMenuItemList.setListData(Find.menuItemByMenuAndStatus(menu, 1).toArray());
        inactiveMenuItemList.setListData(Find.menuItemByMenuAndStatus(menu, 0).toArray());
    }

    private void menuOperation(int i) {
        Object o = i == 0 ? activeMenuList.getSelectedValue() : inactiveMenuList.getSelectedValue();
        if (o == null) {
            return;
        }
        Menu menu = (Menu) o;
        menu = m.find(Menu.class, menu.getMenu());
        menu.setStatus(i);
        m.update(menu);
        refresh();
    }

    private void menuItemOperation(int i) {
        Object o = i == 0 ? activeMenuItemList.getSelectedValue() : inactiveMenuItemList.getSelectedValue();
        if (o == null) {
            return;
        }
        MenuItem menuItem = (MenuItem) o;
        menuItem = m.find(MenuItem.class, menuItem.getMenuItemPK());
        menuItem.setStatus(i);
        m.update(menuItem);
        fillMenuItemList();
        mainView.menuRegister();
    }
}
